#coding:utf8
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from pynput import keyboard
import sys

mode = 1# 0 input mouse pos,2 input step,3 input click,4 skip keys in keyboard mode,5 xing mode

if len(sys.argv )==2:
    mode = int(sys.argv[1])

codes = []
import time
def get_key_str(key):
    s = '%s'%key
    kc = ''
    kv = ''
    try:
        kc = key.char
    except:
        pass
    
    try:
        kv = key.vk
    except:
        pass
        
    return '%s#%s#%s'%(s,kc,kv)
import json
def on_press(key):    
    s = get_key_str(key)
    print(s)
        
    if mode==0:
        codes.append(s)
        print('save',len(codes))
        f = open('c.txt','w')
        json.dump(codes,f)
        f.close()   
        
    if mode==3:
        codes.append(s)
        print('save3',len(codes))
        f = open('c3.txt','w')
        json.dump(codes,f)
        f.close()    
        
    if mode==2:
        codes.append(s)
        print('save2',len(codes))
        f = open('c2.txt','w')
        json.dump(codes,f)
        f.close()   
        
    if mode==4:
        codes.append(s)
        print('save2',len(codes))
        f = open('c4.txt','w')
        json.dump(codes,f)
        f.close()    
    if mode==5:
        codes.append(s)
        print('save2',len(codes))
        f = open('c5.txt','w')
        json.dump(codes,f)
        f.close()   
    if mode==6:
        codes.append(s)
        print('save2',len(codes))
        f = open('c6.txt','w')
        json.dump(codes,f)
        f.close()   
        
    if mode==7:
        codes.append(s)
        print('save2',len(codes))
        f = open('c7.txt','w')
        json.dump(codes,f)
        f.close()   
        
listener = keyboard.Listener(
    on_press=on_press
)
listener.start()


class my(QMainWindow):
    def __init__(self):
        super().__init__()         
        self.show()       
a = QApplication([])   
w = my()
a.exec_()